package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    //查询所有预约信息（分页，条件查询）
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = orderService.findPage(queryPageBean);

            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    //新增预约
    @RequestMapping("/addOrder")
    public Result addOrder(@RequestBody Map map, Integer[] setmealIds) throws Exception {
        Result result = null;
        map.put("orderType",Order.ORDERTYPE_TELEPHONE);
        for (Integer setmealId : setmealIds) {
            map.put("setmealId", String.valueOf(setmealId));
            result = orderService.order(map);
        }
        return result;
    }
    //取消预约
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try{
            orderService.delete(id);
        }catch (Exception e){
            return new Result(false, MessageConstant.CANCEL_ORDER_FAIL);
        }
        return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);
    }
    //修改预约状态
    @RequestMapping("/update")
    public Result update(Integer id) {
        try{
            orderService.update(id);
        }catch (Exception e){
            return new Result(false, MessageConstant.UPDATE_ORDERSTATUS_FAIL);
        }
        return new Result(true, MessageConstant.UPDATE_ORDERSTATUS_SUCCESS);
    }
    //查询地址表返回所有地址信息
    @RequestMapping("/findAllAdr")
    public Result findAllAdr() {
        try{
            List<Map> list = orderService.findAllAdr();
            return new Result(true,"查询地址列表成功",list);
        }catch (Exception e){
            return new Result(false,"查询地址列表失败");
        }
    }
}
